const db = uniCloud.database( )
const dbCmd = db.command

module.exports = {
	async updateShopInfo( params, role ) {
		// 验证权限
		if ( role !== 'admin' ) {
			throw new Error( '无权限访问' )
		}

		const { shop_name, shop_avatar, shop_desc, shop_code } = params

		if ( shop_name !== undefined && !shop_name ) {
			return {
				errCode: 400,
				errMsg: '店铺名称不能为空'
			}
		}

		try {
			// 获取现有店铺信息
			const shop = await db.collection( 'shop' ).limit( 1 ).get( )

			if ( shop.data.length > 0 ) {
				// 构建更新对象
				const updateData = {
					update_date: Date.now( )
				}
				// 只更新传入的字段
				if ( shop_name !== undefined ) updateData.shop_name = shop_name
				if ( shop_avatar !== undefined ) updateData.shop_avatar = shop_avatar
				if ( shop_desc !== undefined ) updateData.shop_desc = shop_desc
				if ( shop_code !== undefined ) updateData.shop_code = shop_code

				await db.collection( 'shop' ).doc( shop.data[ 0 ]._id ).update( updateData )
			} else {
				// 创建新的店铺信息
				await db.collection( 'shop' ).add( {
					...params,
					create_date: Date.now( ),
					update_date: Date.now( )
				} )
			}

			return {
				errCode: 0,
				errMsg: '更新成功'
			}

		} catch ( error ) {
			throw new Error( '更新失败：' + error.message )
		}
	},

	async getShopInfo( ) {
		try {
			const result = await db.collection( 'shop' )
				.limit( 1 )
				.get( )

			return {
				errCode: 0,
				errMsg: '获取成功',
				data: result.data[ 0 ] || null
			}

		} catch ( error ) {
			throw new Error( '获取失败：' + error.message )
		}
	},

	async updateShopSettings( params, role ) {
		// 验证权限
		if ( role !== 'admin' ) {
			throw new Error( '无权限访问' )
		}

		try {
			// 验证参数
			const {
				isOpen,
				autoAcceptOrder,
				minOrderAmount,
				deliveryRange,
				deliveryFee,
				openTime,
				closeTime
			} = params

			// 转换数字类型
			const settings = {
				isOpen: !!isOpen,
				autoAcceptOrder: !!autoAcceptOrder,
				minOrderAmount: Number( minOrderAmount ) || 0,
				deliveryRange: Number( deliveryRange ) || 0,
				deliveryFee: Number( deliveryFee ) || 0,
				openTime: openTime || '',
				closeTime: closeTime || ''
			}

			// 获取现有店铺信息
			const shop = await db.collection( 'shop' ).limit( 1 ).get( )

			if ( shop.data.length > 0 ) {
				// 更新现有店铺设置
				await db.collection( 'shop' ).doc( shop.data[ 0 ]._id ).update( {
					settings,
					update_date: Date.now( )
				} )
			} else {
				// 创建新的店铺信息
				await db.collection( 'shop' ).add( {
					settings,
					create_date: Date.now( ),
					update_date: Date.now( )
				} )
			}

			return {
				errCode: 0,
				errMsg: '更新成功'
			}

		} catch ( error ) {
			throw new Error( '更新失败：' + error.message )
		}
	},

	async getShopSettings( role ) {
		// 验证权限
		if ( role !== 'admin' ) {
			throw new Error( '无权限访问' )
		}

		try {
			const result = await db.collection( 'shop' )
				.get( )

			// 如果没有数据，返回默认值
			if ( !result.data.length ) {
				return {
					errCode: 0,
					errMsg: '获取成功',
					data: {
						isOpen: false,
						autoAcceptOrder: false,
						minOrderAmount: '',
						deliveryRange: '',
						deliveryFee: '',
						openTime: '',
						closeTime: ''
					}
				}
			}

			// 如果有数据但没有settings字段，也返回默认值
			if ( !result.data[ 0 ].settings ) {
				return {
					errCode: 0,
					errMsg: '获取成功',
					data: {
						isOpen: false,
						autoAcceptOrder: false,
						minOrderAmount: '',
						deliveryRange: '',
						deliveryFee: '',
						openTime: '',
						closeTime: ''
					}
				}
			}

			// 返回数据库中的settings
			return {
				errCode: 0,
				errMsg: '获取成功',
				data: result.data[ 0 ].settings
			}

		} catch ( error ) {
			throw new Error( '获取失败：' + error.message )
		}
	}
}